﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ashwani.Cryptography;
using System.Xml;
using Ashwani.Cryptography.Symmetric;
using Ashwani.Cryptography.Hashing;
using Ashwani.Cryptography.Signature;
using System.Security.Cryptography.X509Certificates;
namespace Ashwani.Test.Cryptography
{
    /// <summary>
    /// Summary description for CryptographyTest
    /// </summary>
    [TestClass]
    public class CryptographyTest
    {
        public CryptographyTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        static Cryptographer crypto = null;
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            crypto = new Cryptographer();
            crypto.AddSymmetricProvider("defaultProvider", new MachineKeySymmetricCryptoProvider(), true);
            crypto.AddHashProvider("defaultHashProvider", new MachineKeyHashProvider(), true);
            crypto.AddSignatureProvider("defaultSignatureProvider", new MachineKeySignatureProvider(), true);
        }

        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void PerformEncrptionandDecryption()
        {
            string plainText = "This is sample text to encrypt";
            string encText = crypto.EncryptSymmetric(plainText);
            string decText = crypto.DecryptSymmetric(encText);

            Assert.AreNotEqual(plainText, encText);//validates that the plain and encText are not equal to ensure that encryption is done.
            Assert.AreEqual(plainText, decText);
        }

        [TestMethod]
        public void PerformXMLEncryptionandDecryption()
        {
            string plainXml = "<root>  <securitynumber>    <number>19834209</number>    <expiry>02/02/2002</expiry>  </securitynumber></root>";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(plainXml);
            crypto.EncryptXml(doc, "securitynumber");
            XmlElement encryptedElement = doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;
            Assert.IsNotNull(encryptedElement, "Error occured during Encryption");// If the EncryptedData element was found, encryption is performed


            crypto.DecryptXml(doc);
            XmlElement element = doc.GetElementsByTagName("securitynumber")[0] as XmlElement;
            Assert.IsNotNull(element, "The element was not retrieved back. Error Occured during decryption.");// If the element was found, decryption is performed
        }


        [TestMethod]
        public void PerformHashing()
        {
            string plainText = "This is sample text to encrypt";

            string hashedText = crypto.ComputeHash(plainText);
            Assert.AreNotEqual(plainText, hashedText);//validates that the plain and hashedText are not equal to ensure that hashing is done.

            bool isMatched = crypto.CompareHash(plainText, hashedText);

            Assert.AreEqual(true, isMatched);
        }

        [TestMethod]
        public void PerformHMac()
        {
            string plainText = "This is sample text to encrypt";

            string hashedText = crypto.CreateSignature(plainText);
            Assert.AreNotEqual(plainText, hashedText);//validates that the plain and hashedText are not equal to ensure that hashing is done.

            bool isMatched = crypto.CompareSignature(plainText, hashedText);

            Assert.AreEqual(true, isMatched);
        }
    }
}
